// 处理业务逻辑的中间件, 读取某个 json 文件的数据
const path = require('path')
const fileUtils = require('../util/file_utils')

module.exports = async (context, next) => {
  const url = context.request.url
  let filePath = `../data${url.replace("/api", "")}.json`
  filePath = path.join(__dirname, filePath)
  try {
    context.response.body = await fileUtils.getData(filePath)
  } catch (e) {
    const error = {
      message: "读取的文件资源不存在",
      code: 404
    }
    context.response.body = JSON.stringify(error)
  }
  await next()
}